package org.mili.ticketeer.ui;

import org.mili.application.ui.Portal;
import org.mili.ticketeer.report.ReportEngine;
import org.mili.application.service.MigrationService;
import org.mili.application.service.ServiceException;
import org.mili.application.service.ServiceFactory;
import org.mili.ticketeer.ui.contact.ContactPortal;
import org.mili.ticketeer.ui.group.GroupPortal;
import org.mili.ticketeer.ui.meeting.MeetingPortal;
import org.mili.ticketeer.ui.priority.PriorityPortal;
import org.mili.ticketeer.ui.role.RolePortal;
import org.mili.ticketeer.ui.state.StatePortal;
import org.mili.ticketeer.ui.ticket.TicketPortal;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;

/**
 */
public class Main {
    private JFrame frame;
    private JMenuBar menuBar;
    private JTabbedPane tabbedPane;
    private Set<String> openTabs = new HashSet<String>();
    private ReportEngine reportEngine = new ReportEngine();

    public Main() {
        init();
        frame = new JFrame("Ticketeer");
        frame.setSize(640, 480);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu menu = new JMenu("Administration");
        menuBar.add(menu);
        makeTabbedMenu(menu, "Tickets", new TicketPortal());
        makeTabbedMenu(menu, "Contacts", new ContactPortal());
        makeTabbedMenu(menu, "States", new StatePortal());
        makeTabbedMenu(menu, "Priorities", new PriorityPortal());
        makeTabbedMenu(menu, "Roles", new RolePortal());
        makeTabbedMenu(menu, "Groups", new GroupPortal());
        makeTabbedMenu(menu, "Meetings", new MeetingPortal());
        addReportsMenu();
        addQuitMenu();
        tabbedPane = new JTabbedPane();
        frame.add(tabbedPane);

    }

    private void addQuitMenu() {
        JMenu menu = new JMenu("Exit");
        JMenuItem item = new JMenuItem("Quit");
        menu.add(item);
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                frame.dispose();
            }
        });
        menuBar.add(menu);
    }

    private void addReportsMenu() {
        JMenu menu = new JMenu("Reports");
        JMenuItem item = new JMenuItem("Create");
        menu.add(item);
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    reportEngine.start();
                } catch (ServiceException e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        menuBar.add(menu);
    }

    private void makeTabbedMenu(JMenu menu, String title, final Portal portal) {
        JMenuItem menuItem = new JMenuItem(title);
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!openTabs.contains(portal.getClass().getSimpleName())) {
                    tabbedPane.addTab(portal.getClass().getSimpleName(), portal);
                    openTabs.add(portal.getClass().getSimpleName());
                }
            }
        });
        menu.add(menuItem);
    }

    private void init() {
        try {
            System.out.println("migrate database...");
            ServiceFactory.getService(MigrationService.class).migrate(false);
            System.out.println("    OK.");
        } catch (ServiceException e) {
            halt(e);
        }
    }

    private void halt(ServiceException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });
    }

}
